const fs = require('fs')

const userService = require('../services/user.service')
const fileService = require('../services/file.service')

const { AVATAR_PATH } = require('../constants/file-path')

class UserController {
	async create(ctx, next) {
		// 获取用户请求传递的参数
		const user = ctx.request.body

		// 查询数据
		const result = await userService.create(user)

		// 返回数据
		ctx.body = result
	}

	async avatarInfo(ctx, next) {
		const { userId } = ctx.request.params
		const avatarInfo = await fileService.getAvatarByUserId(userId)

		console.log('avatarInfo: ---', avatarInfo)
		// 返回内容
    ctx.response.set('content-type', avatarInfo.mimetype)
    // 创建读取流
		ctx.body = fs.createReadStream(`${AVATAR_PATH}/${avatarInfo.filename}`)
	}
}

module.exports = new UserController()
